package com.dispacher.springsupport;

import javax.servlet.ServletContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dispacher.context.RequestContext;

abstract class WebApplicationContextSupport {

	private static WebApplicationContext context;

	public static void initWebApplicationContextCache() {
		ServletContext servletContext = RequestContext.getCurSession().getServletContext();
		context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
	}

	public static Object getBean(String beanName) {
		if (context == null) {
			synchronized (WebApplicationContextSupport.class) {
				if (context == null) {
					initWebApplicationContextCache();
				}
			}

		}
		return context.getBean(beanName);
	}
}
